package dao.impl;

import dao.BaseDao;
import dao.PowerBatteryDao;
import entity.PoweBattery;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class PowerBatterDaoImpl extends BaseDao implements PowerBatteryDao {
    public List<PoweBattery> selectAll() {
        List<PoweBattery> list =new ArrayList<>();
        try {
            conn = super.getConnection();
            pstmt = conn.prepareStatement("select id, carName,cityName,userName,pressureValue,time from microblog.powerbattery ORDER BY time desc");
            rs = pstmt.executeQuery();
            while (rs.next()){
                PoweBattery powerbattery = qu(rs);
                list.add(powerbattery);
            }
        }catch (SQLException sqlException){
            sqlException.printStackTrace();
        }
        finally {
            closeAll(conn,pstmt,rs);
        }
        return list;
    }
    private PoweBattery qu(ResultSet rs){
        PoweBattery powebattery = new PoweBattery();
        try{
            powebattery.setId(rs.getInt(1));
            powebattery.setCarName(rs.getString(2));
            powebattery.setCityName(rs.getString(3));
            powebattery.setUserName(rs.getString(4));
            powebattery.setPressureValue(rs.getInt(5));
            powebattery.setTime(rs.getString(6));
        }catch (SQLException q){
            q.printStackTrace();
        }
        return powebattery;
    }

    @Override
    public PoweBattery selectThis(int id) {
        PoweBattery powerbattery = null;
        try {
            conn = super.getConnection();
            pstmt=  conn.prepareStatement("select id, carName,cityName,userName,pressureValue,time from powerbattery where id =?");
            pstmt.setInt(1,id);
            rs = pstmt.executeQuery();
            if (rs.next()){
                powerbattery = qu(rs);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            closeAll(conn,pstmt,rs);
        }
        return powerbattery;
    }

    @Override
    public int selectUpdate(PoweBattery powerbattery) {
        int row = -1;
        try {
            conn = super.getConnection();
            pstmt=  conn.prepareStatement("UPDATE `powerbattery` SET `carName` = ?, `cityName` =?, `userName` =?, `pressureValue` = ?, `time` = ? WHERE `id` = ?");
            pstmt.setInt(6,powerbattery.getId());
            pstmt.setInt(4,powerbattery.getPressureValue());
            pstmt.setString(1,powerbattery.getCarName());
            pstmt.setString(2,powerbattery.getCityName());
            pstmt.setString(3,powerbattery.getUserName());
            pstmt.setString(5,powerbattery.getTime());
            row = pstmt.executeUpdate();
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            closeAll(conn,pstmt);
        }
        return row;
    }

    @Override
    public int delete(int id) {
        int result = -1;
        try {
            conn = super.getConnection();
            pstmt=  conn.prepareStatement("delete from `powerbattery` where id =?");
            pstmt.setInt(1,id);
            result = pstmt.executeUpdate();
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            closeAll(conn,pstmt);
        }
        return result;
    }
}
